package org.javanum.number;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
 * Author: Scott Fines
 * Date: Dec 20, 2009
 */
public class StringFormatTest {

    @Test
    public void testStringFormatting(){
        System.out.println(Double.NaN/2);

        System.out.println(2/Double.NaN);

        System.out.println(Double.NaN/Double.NaN);

        System.out.println(Double.NaN*1);

        System.out.println(Double.NaN*Double.NaN);

        System.out.println(Double.NaN-1);
        System.out.println(1-Double.NaN);
        System.out.println(Double.NaN-Double.NaN);

        System.out.println(Double.isInfinite(Double.NaN));

    }


    @Test
    public void testDoubleHashCodeAndInfinity(){
        double a=1.0;
        double b = 1.0;
        double c = Double.POSITIVE_INFINITY;
        double d = Double.POSITIVE_INFINITY;

        int prime = 31;
        int result1 = 17;
        int result2 = 17;

        long temp1 = Double.doubleToLongBits(a);
        long temp2 = Double.doubleToLongBits(c);
        assertTrue("Double.doubleToLongBits(Double.POSITIVE_INFINITY" +
                "==Double.doubleToLongBits(1.0)",temp1!=temp2);

        result1 = prime*result1 + (int)(temp1^(temp1>>>32));
        result2 = prime*result2 + (int)(temp2^(temp2>>>32));

        assertTrue("Double.POSITIVE_INFINITY.hashCode()" +
                "==(1.0).hashCode()",result1!=result2);

        temp1 = Double.doubleToLongBits(b);
        temp2 = Double.doubleToLongBits(d);
        assertTrue("Double.doubleToLongBits(Double.POSITIVE_INFINITY" +
                "==Double.doubleToLongBits(1.0)",temp1!=temp2);

        result1 = prime*result1+(int)(temp1^(temp1>>>32));
        result2 = prime*result2+(int)(temp2^(temp2>>>32));

        assertTrue("(1.0,1.0).hashCode()==" +
                "(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY).hashCode()",
                result1!=result2);
    }

    
}
